package fr.ul.ade.secunde.ui.tablet;

import android.app.Activity;

import com.actionbarsherlock.app.SherlockFragmentActivity;

import fr.ul.ade.R;
import fr.ul.ade.secunde.ui.CreateScheduleFragment;
import fr.ul.ade.secunde.ui.EmptyFragment;
import fr.ul.ade.secunde.ui.ScheduleListFragment;

/**
 * Gestionnaire pour l'affichage des fragments pour la version tablette
 * (orientation portrait).
 * 
 * @author Michaël Schnell
 * @since Jul 2, 2013
 * 
 * @version 1.5
 */
public class PortraitManager
		extends FragmentContentManager {

	private static final String TAG_LEFT_FRAGMENT = "left_fragment"; //$NON-NLS-1$

	private static final String TAG_RIGHT_FRAGMENT = "right_fragment"; //$NON-NLS-1$

	public PortraitManager(SherlockFragmentActivity activity) {
		super(activity);
	}

	@Override
	public void initFragments() {
		this.beginTransaction();
		this.showFragment(R.id.left_upper_fragment_container,
				ScheduleListFragment.newInstance(),
				TAG_LEFT_FRAGMENT);
		this.showFragment(R.id.right_fragment_container,
				EmptyFragment.newInstance(),
				TAG_RIGHT_FRAGMENT);
		this.commitTransaction();
	}

	@Override
	public void showCreateForm() {
		super.showCreateForm();
		this.beginTransaction();
		this.hideFragment(TAG_RIGHT_FRAGMENT);
		this.showFragment(R.id.right_fragment_container,
				CreateScheduleFragment.newInstance(),
				TAG_RIGHT_FRAGMENT);
		this.commitTransaction();
	}

	/**
	 * Gère le retour en arrière pour le contenu des fragments.
	 * 
	 * @return {@code true} s'il faut appeler {@link Activity#onBackPressed()},
	 *         {@code false} si l'évènement à été complètement géré par cet
	 *         appel
	 */
	@Override
	public boolean onBackPressed() {
		if (this.state.hasState(FragmentState.CREATE_SCHEDULE)) {
			this.beginTransaction();
			this.hideFragment(TAG_RIGHT_FRAGMENT);
			this.showFragment(R.id.middle_fragment_container,
					EmptyFragment.newInstance(),
					TAG_RIGHT_FRAGMENT);
			this.commitTransaction();
			return super.onBackPressed();
		}

		// Other states : leave activity
		// - FragmentState.SCHEDULE_LIST
		return super.onBackPressed();
	}

}// Class PortraitManager